package jbstoremgmt.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import jbstoremgmt.dao.BPDAO;
import jbstoremgmt.domain.Address;
import jbstoremgmt.domain.BPMaster;
import jbstoremgmt.service.BPDetailsBean;
import jbstoremgmt.util.UtilHelper;

//import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

//import org.apache.commons.beanutils.PropertyUtils;

public class BPController extends SimpleFormController{
	
	private BPDAO bpDAO;
	
	public void setBpDAO(BPDAO bpDAO)
	{
		this.bpDAO = bpDAO;
	}
	
	public Object formBackingObject(HttpServletRequest request) throws ServletException
	{
		BPDetailsBean backingObject = new BPDetailsBean();
		//BPMaster bpMaster = bpDAO.getBP(2010902);
		BPMaster bpMaster = new BPMaster();
		bpMaster.setBp_address(new Address());
		
		
		
		
		try
		{
				// The following line is Temporarily Commented to load an empty bean
			   UtilHelper.copyProperties(backingObject, bpMaster);
		}
		catch(Exception e)
		{
			System.out.println("Exception occured in BPController formBackingObject");
		}
		
		List<String> chkboxlist = new ArrayList<String>();
		
		chkboxlist.add("element1");
		chkboxlist.add("element2");
		chkboxlist.add("element3");
		chkboxlist.add("element4");
		chkboxlist.add("element5");
		
		backingObject.setChkboxlist(chkboxlist);
		
		
	   List<String> selectedList = new ArrayList<String>();
		
	   selectedList.add("element1");
	   selectedList.add("element2");
	   selectedList.add("element3");
		
	   backingObject.setSelectedlist(selectedList);
		return backingObject;
	}
	

public void doSubmitAction(Object command)
	{
		try
		{
			BPDetailsBean  bpDetailsBean = (BPDetailsBean)command;
			if(bpDetailsBean!=null)
			{
				BPMaster bpMaster = new BPMaster();
				UtilHelper.copyProperties(bpMaster,bpDetailsBean);
				
				bpDAO.saveBP(bpMaster);
			}
		}
		catch (Exception e) {
			// TODO: handle exception
		}
	}

	/* The below method can be used if we need to move to another page rather than the one
	 * defined in the successView property page.  
	 */
 
	/*public ModelAndView onSubmit(Object command) throws ServletException
	{
		LoginFormBean  lBean = (LoginFormBean)command;
		System.out.println("username :"+lBean.getUsrid());
		System.out.println("password :"+lBean.getUsrpwd());
		//Now you can validate to database
		return new ModelAndView("success");
	}
	*/	
}
